package com.tj.collection.application;

import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.tj.collection.bean.RedisKey;
import com.tj.collection.config.NacosValueConfig;
import com.tj.collection.db.admin.dao.AdminDAO;
import com.tj.collection.db.domain.Admin;
import com.tj.collection.db.service.impl.AdminLoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Created by xiatian on 2017/10/16.
 */
@Component
public class AdminApplication {

    private static final Log log = LogFactory.getLog(AdminApplication.class);
    @Autowired
    private AdminDAO adminDAO;
    @Autowired
    private AdminLoginService adminLoginService;
    @Autowired
    private RedisClient redisClient;

    @Autowired
    private NacosValueConfig nacosValueConfig;

    /**
     * @param admin
     * @param sso   是否单点登录
     * @return
     */
    public String login(Admin admin, boolean sso) {
        if (admin == null) {
            throw new ApplicationException("The user does not exist!");
        }

        //短信验证码登录白名单，加入白名单无需输入验证码即可登录（仅限于测试环境）
        String loginWhiteList = nacosValueConfig.getLoginWhiteAccountList();
        if (StringUtils.isNotBlank(loginWhiteList)) {
            String accounts[] = loginWhiteList.split(";");
            for (String account : accounts) {
                if (account.equals(admin.getAccount())) {
                    String token = adminLoginService.adminLogin(admin, "VERIFYCODE", false);
                    return token;
                }
            }
        }

        String token = adminLoginService.adminLogin(admin, null, sso);
        return token;
    }


}
